﻿using System;
using System.Data;
using System.Data.Common;
using System.Xml.XPath;
using Songhay.Data.GenericWeb.Properties;
using Songhay.Xml;

namespace Songhay.Data.GenericWeb.Procedure
{
    /// <summary>
    /// Delete procedures for Web-schema data.
    /// </summary>
    internal static partial class Delete
    {
        internal static XPathDocument Fragment(DbConnection commonConnection, IXPathNavigable navigableInput)
        {
            if(commonConnection == null) throw new ArgumentNullException("commonConnection", "The Common Connection is null.");

            XPathNavigator node = XmlUtility.GetNavigableNode(navigableInput, "//FormData");
            Object p = XmlUtility.GetNodeValue(node, "./FragmentId", true, DBNull.Value);

            string id = p.ToString();

            using(DbCommand cmd = commonConnection.CreateCommand())
            {
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = Resources.DeleteFragment;

                DbParameter parameter;

                parameter = cmd.CreateParameter();
                parameter.ParameterName = "@FragmentId";
                parameter.Value = id;
                cmd.Parameters.Add(parameter);

                cmd.ExecuteNonQuery();
            }

            string[] sa = new string[] { string.Format("Fragment {0} has been removed.", id) };
            return XmlUtility.GetInternalMessageDocument("Item Deleted from the Database", sa);
        }
    }
}
